const { Pool } = require('pg');


// 配置数据库连接参数
const pool = new Pool({
    user: 'postgres',
    password: '123456',
    host: '192.168.3.211',
    port: 5433,
    database: 'xizangbiaoshu'
});


class PGutils {
    constructor() {

    }


    async query(sql, params) {
        const client = await pool.connect();
        try {
             
            console.log('Connected to PostgreSQL database' + sql);
            // 执行查询
            const res = await client.query(sql);
            console.log('Query result:', res.rows[0]);
            if(res){
                return res.rows[0];
            }
            return null;
        }catch(error){    
            console.log('Connected to PostgreSQL database error'+ error.message);
        } finally {
            
            client.release();
        }

    }

    async insert(sql, values) {
        console.log(`打印sql >>> ${sql} 打印参数 ${values}`)
        const client = await pool.connect();
        // 连接到数据库
        try {
           
            console.log('Connected to PostgreSQL database');
            // 执行查询
            client.query(sql, values, (err, res) => {
                if (err) {
                    console.error(err);
                } else {
                   
                }
            });
        }catch(error){    
            console.log('Connected to PostgreSQL database error'+ error.message);
        } finally {
            client.release();
        }
    }
}



module.exports = new PGutils();